const puppeteer = require('puppeteer');

const sd = require('silly-datetime');
var time = sd.format(new Date(), 'YYYY-MM-DDHHmmss');
console.log(time);

// (async () => {
//     const browser = await puppeteer.launch({ headless: true, slowMo: 250 })
//     const page = await browser.newPage();
//     await page.goto('https://juejin.im/post/59e5a86c51882578bf185dba');
// await page.setViewport({
//     width :1600,
//     //设置成0，默认界面最大化
//     height :0
// })
//     await page.screenshot({ path: time + 'example.png' });
//     await page.pdf({ path: time + 'example.pdf', format: 'A4' });
//     await browser.close();
// })();

const start = document.getElementById('start');
start.addEventListener('click', function(event) {
    var luntan = document.getElementById('luntan');
    var luntantext = luntan.options[luntan.selectedIndex].value;
    console.log(luntantext);
    if (luntantext == '天涯') {
        tianya();
    }
    if (luntantext == '新浪微博') {
        for (var index = 0; index <= 5; index++) {}
        xinlang(1);
    }
})
function ceshi() {
    (async () => {
        const pathToExtension = require('path').join(__dirname, 'chrome-mac/Chromium.app/Contents/MacOS/Chromium');
        const browser = await puppeteer.launch({
            headless: false,
            // executablePath: pathToExtension
        });
        const page = await browser.newPage();
        await page.goto('https://www.baidu.com');
        await page.setViewport({width: 1000, height: 500});
        await page.screenshot({path: 'example.png'});
        await browser.close();
    })();
}
function tianya() {
    (async () => {
        const browser = await puppeteer.launch({
            headless: false,
            ignoreHTTPSErrors: true, //如果是访问https页面 此属性会忽略https错误
            // 打开开发者工具, 当此值为true时, headless总为false
            devtools: false,
        });
        const page = await browser.newPage();
        await page.goto('http://bbs.tianya.cn/compose.jsp');
        await page.setViewport({
            width: 500,
            //设置成0，默认界面最大化
            height: 600
        })
        await page.type('#vwriter', '18729036400', { delay: 100 });
        await page.type('#vpassword', 'y1010903236Q', { delay: 100 });
        page.click('#topguideloginform > div:nth-child(4) > button')
        await page.waitFor(1000);
        await page.screenshot({ path: time + '百度.png', type: 'png', fullPage: true, });
        /*const targetLink = await page.evaluate(() => {
            return [...document.querySelectorAll('.result a')].filter(item => {
                return item.innerText && item.innerText.includes('Puppeteer的入门和实践')
            }).toString()
        });*/
        // await page.goto(targetLink);
       /* await page.waitFor(1000);
        // await page.waitForSelector('#loginForm');//等待某个节点出现
        // await page.waitForNavigation();
        await page.type('#BBS_BLOCK > table > tbody > tr:nth-child(1) > td:nth-child(2) > input', '你们想知道最近有什么好玩的东西吗？', { delay: 10 });
        await page.type('#textAreaContainer', '哈哈今天是愚人节啊  你们呗我骗了呀哈哈 我成功了', { delay: 10 });
        page.click('#BBS_BLOCK > table > tbody > tr:nth-child(3) > td:nth-child(2) > span.fr.mr10.mt5 > label.mr10 > input[type="radio"]');
        page.click('#BBS_BLOCK > table > tbody > tr:nth-child(1) > td:nth-child(2) > span > select');

        await page.waitFor(10000);
        page.click('#BBS_BLOCK > table > tbody > tr:nth-child(1) > td:nth-child(2) > span > select > option:nth-child(2)');

        page.click('#BBS_BLOCK > table > tbody > tr:nth-child(3) > td:nth-child(2) > button.common-submitBtn.fr');
        await page.waitFor(10000);*/
        //browser.close();
    })()
}

function xinlang(index) {
    var content = `我知道他的意思,就是为什么v总是2但是这个不叫异步循环(我第一次听到这样奇葩的名字)`;
    addHtml('message', "新浪微博正在发帖中...请等待。。。发帖内容为：<br>" + content);
    (async () => {
        const browser = await puppeteer.launch({
            headless: true, //如果是访问https页面 此属性会忽略https错误
            ignoreHTTPSErrors: true,
            // 打开开发者工具, 当此值为true时, headless总为false
            devtools: false,
        });
        const page = await browser.newPage();
        await page.goto('https://weibo.com/');
        await page.setViewport({
            width: 0,
            //设置成0，默认界面最大化
            height: 0
        })
        addHtml('message', "正在加载登录界面！......");
        await page.waitForSelector('#loginname');
        await page.type('#loginname', '');
        await page.waitFor(1000);
        addHtml('message', "正在填写用户名！......");
        await page.type('#loginname', '18729036400', { delay: 100 });
        await page.waitForSelector('#pl_login_form');
        addHtml('message', "正在填写密码！......");
        await page.type('#pl_login_form > div > div:nth-child(3) > div.info_list.password > div > input', 'yq1010903236', { delay: 100 });

        await page.waitForSelector('#pl_login_form');
        page.click('#pl_login_form > div > div:nth-child(3) > div.info_list.login_btn > a')
        await page.screenshot({ path: time + '新浪.png', type: 'png', fullPage: true, });
        /*const targetLink = await page.evaluate(() => {
            return [...document.querySelectorAll('.result a')].filter(item => {
                return item.innerText && item.innerText.includes('Puppeteer的入门和实践')
            }).toString()
        });*/
        // await page.goto(targetLink);
        await page.waitFor(1000);
        addHtml('message', "等待发帖页面显示中......");
        await page.waitForSelector('#v6_pl_content_publishertop > div > div.input > textarea');
        // await page.waitForSelector('#loginForm');//等待某个节点出现
        // await page.waitForNavigation();
        addHtml('message', "正在填写发帖内容！");
        await page.type('#v6_pl_content_publishertop > div > div.input > textarea', time + content, { delay: 10 });

        page.click('#v6_pl_content_publishertop > div > div.func_area.clearfix > div.func > a');
        await page.waitFor(10000);
        browser.close();
        console.log(index + "完成了！");
        addHtml('message', "发帖完成了！");
    })()
}

function addHtml(pid, content) {
    var info = document.createElement('div');
    info.innerHTML = content + '<br>';
    info.style.color = "gray";
    document.getElementById(pid).appendChild(info);
}
